/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import model.FaktureretVare;
import model.Vare;
import model.Varegruppe;
import view.GUI;

/**
 *
 * @author Morten J, Morten B & Jonas
 */
public class StatestikHandler {

    private DefaultTableModel model;
    private ResultSet rs;
    private ArrayList<FaktureretVare> statFaktureretVare;

    public StatestikHandler(DefaultTableModel model) {
	this.model = model;
    }

    public void createStatestik(String date) {

	boolean resultsFound = false;

	String sql = "SELECT * from faktura "
		+ "right outer join faktureretVare on fakturaNr = fk_fakturaNr "
		+ "inner join vare on fk_vareNr = varenr "
		+ "where date_format(from_unixtime(fakturaDato), '%d-%m-%Y') = '" + date + "'";
	try {
	    rs = Main.getDbConnection().select(sql);
	    statFaktureretVare = new ArrayList<>();
	    Varegruppe varegruppe = null;
	    while (rs.next()) {
		for (Varegruppe vg : VaregruppeHandler.getVareGruppeListe()) {
		    if (rs.getInt("fk_vareGruppe") == vg.getVareGruppeNr()) {
			varegruppe = vg;
		    }
		}
		resultsFound = true;
		Vare vare = new Vare(rs.getInt("vareNr"), rs.getString("vareNavn"),
			rs.getString("leverandoer"), rs.getDouble("salgsPrisKr"),
			rs.getDouble("salgsPrisEu"), rs.getBoolean("medlemspris"),
			rs.getInt("rabat"), rs.getInt("vareBeholdning"),
			rs.getBoolean("aktuel"), varegruppe);

		FaktureretVare ftVare = new FaktureretVare(rs.getInt("antal"),
			rs.getDouble("prisHistorikKr"), rs.getDouble("prisHistorikEu"), vare);

		statFaktureretVare.add(ftVare);
	    }

	} catch (SQLException ex) {
	    GUI.getGuiHandler().setFeedbackMessage(" ved indlæsning af statistik");
	} catch (NullPointerException ex) {
	    GUI.getGuiHandler().setFeedbackMessage(" ved indlæsning af statistik");
	}
	int adults = 0;
	int kids = 0;
	int free = 0;

	for (FaktureretVare faktureretVare : statFaktureretVare) {
	    if (faktureretVare.getVare().getVareNr() == 31) {
		adults = adults + faktureretVare.getAntal();
	    } else if (faktureretVare.getVare().getVareNr() == 32) {
		free = free + faktureretVare.getAntal();
	    } else if (faktureretVare.getVare().getVareNr() == 33) {
		kids = kids + faktureretVare.getAntal();
	    }
	}
	updateModel(adults, 0, 1);
	updateModel(kids, 1, 1);
	updateModel(free, 2, 1);

	if (resultsFound) {
	    GUI.getGuiHandler().setFeedbackMessage("Viser besøgende d. " + date, Main.getGREEN_COLOR());
	} else {
	    GUI.getGuiHandler().setFeedbackMessage("Ingen besøgende d. " + date, Main.getGREEN_COLOR());
	}
    }

    public void updateModel(int value, int row, int column) {
	model.setValueAt(value, row, column);
    }
}
